package com.macro.mall.portal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.macro.mall.mgb.mapper.*;
import com.macro.mall.mgb.model.*;
import com.macro.mall.portal.dao.HomeDao;
import com.macro.mall.portal.domain.*;
import com.macro.mall.portal.service.HomeService;
import com.macro.mall.portal.util.DateUtil;
import com.macro.mall.portal.util.GenericConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author wangwang
 * @version 1.0
 * @description 首页内容管理接口
 * @date 2025/7/25 20:40
 */
@Service
@Slf4j
public class HomeServiceImpl implements HomeService {

    @Autowired
    private SmsHomeAdvertiseMapper smsHomeAdvertiseMapper;

    @Autowired
    private SmsFlashPromotionMapper smsFlashPromotionMapper;

    @Autowired
    private SmsFlashPromotionSessionMapper smsFlashPromotionSessionMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    @Autowired
    private HomeDao homeDao;

    @Override
    public HomeContentResult content() {
        HomeContentResult result = new HomeContentResult();
        //获取首页广告
        result.setAdvertiseList(getSmsHomeAdvertiseList());
        //获取秒杀信息
        result.setHomeFlashPromotion(getHomeFlashPromotion());
        //获取推荐品牌
        result.setBrandList(GenericConverter.convertList(homeDao.getRecommendBrandList(0, 6), BrandDto.class));
        //获取新品推荐
        result.setNewProductList(GenericConverter.convertList(homeDao.getNewProductList(0, 4), HomeProductDto.class));
        //获取人气推荐
        result.setHotProductList(GenericConverter.convertList(homeDao.getHotProductList(0, 4), HomeProductDto.class));
        //获取推荐专题
        result.setSubjectList(homeDao.getRecommendSubjectList(0, 4));
        return result;
    }

    @Override
    public List<Product> recommendProductList(Integer pageSize, Integer pageNum) {
        Page<Product> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Product::getDeleteStatus, 0)
                .eq(Product::getPublishStatus, 1);
        Page<Product> products = productMapper.selectPage(page, queryWrapper);
        List<Product> records = products.getRecords();
        return records;
    }

    @Override
    public List<ProductCategoryDto> getProductCateList(Long parentId) {
        LambdaQueryWrapper<ProductCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductCategory::getParentId, parentId).eq(ProductCategory::getShowStatus,1);
        List<ProductCategory> categoryList = productCategoryMapper.selectList(queryWrapper);
        return GenericConverter.convertList(categoryList, ProductCategoryDto.class);
    }

    @Override
    public List<HomeProductDto> hotProductList(Integer pageNum, Integer pageSize) {
        int offset = pageSize * (pageNum - 1);
        List<Product> hotProductList = homeDao.getHotProductList(offset, pageSize);
        return GenericConverter.convertList(hotProductList,HomeProductDto.class);
    }

    @Override
    public List<HomeProductDto> newProductList(Integer pageNum, Integer pageSize) {
        int offset = pageSize * (pageNum - 1);
        List<Product> newProductList = homeDao.getHotProductList(offset, pageSize);
        return GenericConverter.convertList(newProductList,HomeProductDto.class);
    }

    //获取秒杀信息
    public HomeFlashPromotion getHomeFlashPromotion() {
        //首页秒杀信息
        HomeFlashPromotion homeFlashPromotion = new HomeFlashPromotion();
        Date now = new Date();
        //获取秒杀活动
        SmsFlashPromotion flushPromotion = getFlashPromotion(now);
        if (flushPromotion != null) {
            //获取当前时间段秒杀场次
            SmsFlashPromotionSession flushPromotionSession = getFlashPromotionSession(now);
            if (flushPromotionSession != null) {
                homeFlashPromotion.setStartTime(flushPromotionSession.getStartTime());
                homeFlashPromotion.setEndTime(flushPromotionSession.getEndTime());
                //获取下一个秒杀场次
                SmsFlashPromotionSession nextFlashPromotionSession = getNextFlashPromotionSession(homeFlashPromotion.getEndTime());
                if (nextFlashPromotionSession != null) {

                    homeFlashPromotion.setNextStartTime(nextFlashPromotionSession.getStartTime());
                    homeFlashPromotion.setNextEndTime(nextFlashPromotionSession.getEndTime());
                }
                List<HomeProductDto> flashProductList = GenericConverter.convertList(homeDao.getFlashProductList(flushPromotion.getId(), flushPromotionSession.getId()), HomeProductDto.class);
                if (!flashProductList.isEmpty()) {
                    homeFlashPromotion.setProductList(flashProductList);
                }
            }
        }
        return homeFlashPromotion;
    }

    //获取下一个秒杀场次
    private SmsFlashPromotionSession getNextFlashPromotionSession(Date endTime) {
        Date date = DateUtil.getTime(endTime);
        LambdaQueryWrapper<SmsFlashPromotionSession> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ge(SmsFlashPromotionSession::getStartTime, date).eq(SmsFlashPromotionSession::getStatus, 1);
        List<SmsFlashPromotionSession> nextFlashPromotionSession = smsFlashPromotionSessionMapper.selectList(queryWrapper);
        if (!nextFlashPromotionSession.isEmpty()) {
            return nextFlashPromotionSession.get(0);
        }
        return null;
    }

    //根据时间获取秒杀活动
    private SmsFlashPromotion getFlashPromotion(Date now) {
        Date date = DateUtil.getDate(now);
        LambdaQueryWrapper<SmsFlashPromotion> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.le(SmsFlashPromotion::getStartDate, date)
                .ge(SmsFlashPromotion::getEndDate, date)
                .eq(SmsFlashPromotion::getStatus, 1);
        List<SmsFlashPromotion> flashPromotions = smsFlashPromotionMapper.selectList(queryWrapper);
        if (!flashPromotions.isEmpty()) {
            return flashPromotions.get(0);
        }
        return null;
    }

    //根据时间获取秒杀场次
    private SmsFlashPromotionSession getFlashPromotionSession(Date now) {
        Date date = DateUtil.getTime(now);
        LambdaQueryWrapper<SmsFlashPromotionSession> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.le(SmsFlashPromotionSession::getStartTime, date)
                .ge(SmsFlashPromotionSession::getEndTime, date)
                .eq(SmsFlashPromotionSession::getStatus, 1);
        List<SmsFlashPromotionSession> promotionSessionList = smsFlashPromotionSessionMapper.selectList(queryWrapper);
        if (!promotionSessionList.isEmpty()) {
            return promotionSessionList.get(0);
        }
        return null;
    }

    //获取首页广告
    private List<SmsHomeAdvertise> getSmsHomeAdvertiseList() {
        LambdaQueryWrapper<SmsHomeAdvertise> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SmsHomeAdvertise::getStatus, 1).eq(SmsHomeAdvertise::getType, 1);
        List<SmsHomeAdvertise> advertises = smsHomeAdvertiseMapper.selectList(queryWrapper);
        return advertises;
    }
}
